//
//  LAGTrackPolyLineCreater.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/12/5.
//  Copyright © 2017年 2Bulu. All rights reserved.
//

#import "LAGTrackPolyLineCreater.h"
#import <MapGisBusiness/MapGisBusiness.h>


#define REGION_EXPANDING_RADIUS (1.5f)



@implementation LAGTrackPolyLineCreater



+(TrackPolyline *)cachedPolyLineForTrackSegment:(TrackSegment *)segment
                               with3rdValueType:(TrackPolylinePointSecondValueType)valType
                            ofCoordinateSysytem:(LACoordinateSystem)mapType
                                      mapRegion:(MKCoordinateRegion *)mapRegion
{
    return [self getTrackPolylineForSegment:segment.pointsSegment withSecondValueType:valType ofCoordinateSysytem:mapType mapRegion:mapRegion];
}

+(TrackPolyline *)getTrackPolylineTrackSegment:(TrackSegment *)segment
                         withSecondValueType:(TrackPolylinePointSecondValueType)valType
                         ofCoordinateSysytem:(LACoordinateSystem)mapType
                                   mapRegion:(MKCoordinateRegion *)mapRegion
{
    TrackPolyline *l = [self getTrackPolylineForSegment:segment.pointsSegment withSecondValueType:valType ofCoordinateSysytem:mapType mapRegion:mapRegion];
    
    return l;
}

+(TrackPolyline *)getTrackPolylineForSegment:(LocationPointsSegmentType)segmentData
                         withSecondValueType:(TrackPolylinePointSecondValueType)valType
                         ofCoordinateSysytem:(LACoordinateSystem)mapType
                                   mapRegion:(MKCoordinateRegion *)mapRegion
{
    if (segmentData.pointsCount<2) {
        return nil;
    }
    
    
    float *scdVals = NULL;
    
    
    NSUInteger numberOfPoints = segmentData.pointsCount;
    NSInteger i = 0;
    
    MKMapPoint *mapPoints  = calloc(numberOfPoints, sizeof(MKMapPoint));
    if(valType != TrackPolylinePointSecondValueTypeNone)
        scdVals = calloc(numberOfPoints, sizeof(float));
    
    
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;
    for (i = 0; i < numberOfPoints; i++)
    {
        LocationPointType point = segmentData.dataBuffer[i];
        
        CLLocationCoordinate2D coordinate = mapCoordinateFromGpsCoordinate(CLLocationCoordinate2DMake(point.latitude, point.longitude), mapType);

        MKMapPoint point1 = MKMapPointForCoordinate(coordinate);
        mapPoints[i] = point1;
        
        if(valType == TrackPolylinePointSecondValueTypeeSpeed)
            scdVals[i] = (float)point.speed;
        else if(valType == TrackPolylinePointSecondValueTypeAltitude)
            scdVals[i] = (float)point.altitude;
        
        if (mapRegion) {
            
            if (coordinate.latitude > maxLat) {
                maxLat = coordinate.latitude;
            }
            if (coordinate.longitude > maxLon) {
                maxLon = coordinate.longitude;
            }
            if (coordinate.latitude < minLat) {
                minLat = coordinate.latitude;
            }
            if (coordinate.longitude < minLon) {
                minLon = coordinate.longitude;
            }
        }
        
        
#if DEBUG
        //不可随意打开，严重影响效率
        //  NSLog(@"point %.2f,%@ (%lf,%lf) ha:%f spped:%.3f",point.timeInterval,[[NSDate dateWithTimeIntervalSince1970:point.timeInterval] appDateDescription],point.longitude,point.latitude,point.accuracy,point.speed);
        
#endif
        
    }
    
    if (mapRegion) {
        CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(minLat + (maxLat - minLat)/2, minLon + (maxLon- minLon)/2);
        MKCoordinateSpan span = MKCoordinateSpanMake((maxLat - minLat),  (maxLon- minLon));
        MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);
        *mapRegion = region;
    }
    
    
    TrackPolyline *trackLines;
    
    if(scdVals)
    {
        trackLines = [ValueAddedTrackPolyLine polylineWithPoints:mapPoints PointsValues:scdVals count:numberOfPoints];
        free(scdVals);
    }
    else
    {
        trackLines = [TrackPolyline polylineWithPoints:mapPoints count:numberOfPoints];
    }
    
    free(mapPoints);
    
    
    return trackLines;
}

/*
+(NSArray <TrackPolyline *>*)getTrackPolylinesFromTrack:(LAGTrackData *)data
                                    withSecondValueType:(TrackPolylinePointSecondValueType)valType
                                    ofCoordinateSysytem:(LACoordinateSystem)mapType
                                              mapRegion:(MKCoordinateRegion *)mapRegion
                                              withBlock:(void (^)(TrackPolyline *line, MKCoordinateRegion currentRegion, BOOL finished))block
{
    
    NSArray * trackSegments = data.trackSegments;
    NSMutableArray * polylines = [[NSMutableArray alloc] initWithCapacity:trackSegments.count];
    
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;
    
    
    for(TrackSegment *segObj in trackSegments)
    {
        MKCoordinateRegion region;
        TrackPolyline * trackLines = [self getTrackPolylineForSegment:segObj.pointsSegment
                                                  withSecondValueType:valType
                                                  ofCoordinateSysytem:mapType
                                                            mapRegion:&region];
        if(!trackLines)
            continue;
        
        
        //计算轨迹的范围
        maxLat = MAX(maxLat, region.center.latitude + region.span.latitudeDelta/2.0f);
        maxLon = MAX(maxLon, region.center.longitude + region.span.longitudeDelta/2.0f);
        minLat = MIN(minLat, region.center.latitude - region.span.latitudeDelta/2.0f);
        minLon = MIN(minLon, region.center.longitude - region.span.longitudeDelta/2.0f);
        
        [polylines addObject:trackLines];
        
        
        if (mapRegion)
        {
            CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(minLat + (maxLat - minLat)/2.0f, minLon + (maxLon- minLon)/2.0f);
            
            CLLocationDegrees lat = (maxLat - minLat)*REGION_EXPANDING_RADIUS;
            lat = lat < 180 ? (lat > 0 ? lat : 0.001) : 180;
            CLLocationDegrees lon = (maxLon - minLon)*REGION_EXPANDING_RADIUS;
            lon = lon < 360 ? (lon > 0 ? lon : 0.001) : 360;
            MKCoordinateSpan span = MKCoordinateSpanMake(lat,  lon);
            MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);
            *mapRegion = region;
        }
        
        
        if([trackSegments indexOfObject:segObj] == trackSegments.count-1)
        {
            if(block)
            {
                block(trackLines,*mapRegion,YES);
            }
            break;
        }
        else
        {
            if(block)
                block(trackLines,*mapRegion,NO);
        }
        
    }
    
    
    return polylines;
}
*/

@end
